package com.example.demo7;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button add,delete,update,query;
    private TextView txt;
    private ContentResolver resolver;   //ContentResolver对象
    private Uri mUri;       //Uri变量
    private ContentValues values;   //传入参数的变量
    private static final String sqlname="info";     //定义数据库表的名称
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }
    //初始化组件
    private void initView() {
        add=findViewById(R.id.add);
        delete=findViewById(R.id.del);
        update=findViewById(R.id.update);
        query=findViewById(R.id.select);
        add.setOnClickListener(this);
        delete.setOnClickListener(this);
        update.setOnClickListener(this);
        query.setOnClickListener(this);
        txt=findViewById(R.id.text);
    }
    //初始化数据
    private void initData() {
        MyDbHelper helper=new MyDbHelper(this);
        SQLiteDatabase database=helper.getWritableDatabase();
        //初始化时，先向数据库插入几个数据
        for (int i = 0; i <3 ; i++) {
            values=new ContentValues();
            values.put("name","stu-"+i);
            database.insert(sqlname,null,values);
        }
        database.close();
    }


    @Override
    public void onClick(View view) {
        //创建内容提供者的解析对象
        resolver=getContentResolver();
        //获取Uri路径
        mUri=Uri.parse("content://com.example.demo7/"+sqlname);
        //新建一个ContentValues对象
        values=new ContentValues();

        switch (view.getId())
        {
            case R.id.add:
                Random random=new Random();
                values.put("name","stu-"+random.nextInt(10));
                resolver.insert(mUri,values);
                Toast.makeText(this, "添加成功！", Toast.LENGTH_SHORT).show();
                Log.d("数据库","添加");
                break;
            case R.id.del:
                resolver.delete(mUri,"name=?",new String[]{"stu-2"});
                Toast.makeText(this, "删除成功！", Toast.LENGTH_SHORT).show();
                Log.d("数据库","删除");
                break;
            case R.id.update:
                values.put("name","update_01");
                int id = resolver.update(mUri, values, "id=?", new String[]{"1"});
                Toast.makeText(this, "修改成功！", Toast.LENGTH_SHORT).show();
                Log.d("数据库","修改");
                break;
            case R.id.select:
                Cursor query = resolver.query(mUri, new String[]{"id", "name"}, null, null, null);
                String str="";
                while (query.moveToNext())
                {
                    str+=query.getInt(0)+"....";
                    str+=query.getString(1)+"\n";
                }
                txt.setText(str);
                break;
        }
    }
}